package model

type Relationship struct {
	Id int64 `xorm:"'id'"`
	G  int64 `xorm:"INTEGER notnull 'g'"`
	M  int64 `xorm:"INTEGER notnull 'm'"`
}

func (this *Relationship) IsCp(g, m int64) (has bool, err error) {
	var w = &Relationship{
		G: g,
		M: m,
	}
	has, err = engine.Get(w)
	return has, err
}
func (this *Relationship) Cp(g, m int64) (err error) {
	has, err := this.IsCp(g, m)
	if err != nil {
		return err
	}
	if has {
		return nil
	}
	var cps = make([]Relationship, 2)
	cps[0] = Relationship{G: g, M: m}
	cps[1] = Relationship{G: m, M: g}
	_, err = engine.Insert(&cps)
	return err
}
func (this *Relationship) Delcp(g, m int64) (err error) {
	_, err = engine.Where("(g=? AND m=?) OR (g=? AND m=?)", g, m, m, g).Delete(this)
	return err
}
func (this *Relationship) FindCp(g int64) (cpInfo *Member, err error) {
	var w = &Relationship{
		G: g,
	}
	has, err := engine.Get(w)
	if err != nil {
		return nil, err
	}
	if has {
		var m Member
		cpInfo, err = m.FindOneById(w.M)
	}
	return cpInfo, err
}
